#include "stdafx.h"
#include "wf_drawlineparallelthrough.h"

#include "pf_creation.h"
#include "pf_snapper.h"



WF_DrawLineParallelThrough::WF_DrawLineParallelThrough(
    PF_Container& container,
    PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw Parallels", container, graphicView) {

    parallel = NULL;
    entity = NULL;
    distance = 1.0;
    number = 1;
    coord = PF_Vector(false);
}


QAction* WF_DrawLineParallelThrough::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Par&allel through point"),
    QAction* action = new QAction(tr("Parallel through point"), NULL);
	action->setIcon(QIcon(":/extui/linesparathrough.png"));
    //action->zetStatusTip(tr("Draw parallel through a given point"));
    return action;
}

void WF_DrawLineParallelThrough::trigger() {
    PF_PreviewActionInterface::trigger();

    if (entity!=NULL) {
        PF_Creation creation(container, graphicView);
        PF_Entity* e = creation.createParallelThrough(coord,
                       number,
                       entity);

        if (e==NULL) {
            PF_DEBUG->print("PF_ActionDrawLineParallelThrough::trigger:"
                            " No parallels added\n");
        }
    }
}



void WF_DrawLineParallelThrough::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionDrawLineParallelThrough::mouseMoveEvent begin");


    switch (getStatus()) {
    case SetEntity: {
            entity = catchEntity(e, LX_Define::ResolveAll);
            /*deletePreview();

            PF_Creation creation(preview, NULL, false);
            creation.createParallelThrough(coord,
                                           number,
                                           entity);

            drawPreview();*/
        }
        break;

    case SetPos: {
            coord = snapPoint(e);
            //PF_Vector(graphicView->toGraphX(e->x()),
            //                  graphicView->toGraphY(e->y()));
            deletePreview();

            PF_Creation creation(preview, NULL, false);
            creation.createParallelThrough(coord,
                                           number,
                                           entity);

            drawPreview();
        }
        break;

    default:
        break;
    }

    PF_DEBUG->print("PF_ActionDrawLineParallelThrough::mouseMoveEvent end");
}



void WF_DrawLineParallelThrough::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        switch (getStatus()) {
        case SetEntity:
            entity = catchEntity(e, LX_Define::ResolveAll);
            if (entity!=NULL) {
                entity->setHighlighted(true);
                graphicView->drawEntity(entity);
                setStatus(SetPos);
            }
            break;
        case SetPos: {
                PF_CoordinateEvent ce(snapPoint(e));
                coordinateEvent(&ce);
            }
            break;
        default:
            break;
        }
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        if (entity!=NULL) {
            entity->setHighlighted(false);
            graphicView->drawEntity(entity);
            entity=NULL;
        }
        init(getStatus()-1);
    }
}



void WF_DrawLineParallelThrough::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetPos:
        coord = mouse;
        trigger();
        break;

    default:
        break;
    }
}



void WF_DrawLineParallelThrough::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetEntity:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Select entity"), tr("Cancel"));
        break;

    case SetPos:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify through point"),
                                            tr("Back"));
        break;

    case SetNumber:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Number:"), tr("Back"));
        break;

    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_DrawLineParallelThrough::showOptions() {
    PF_ActionInterface::showOptions();

    PF_DIALOGFACTORY->requestOptions(this, true);
    updateMouseButtonHints();
}



void WF_DrawLineParallelThrough::hideOptions() {
    PF_ActionInterface::hideOptions();

    PF_DIALOGFACTORY->requestOptions(this, false);
}



void WF_DrawLineParallelThrough::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                         + getAvailableCommands().join(", "));
        return;
    }

    switch (getStatus()) {
    case SetEntity:
    case SetPos: {
            if (checkCommand("number", c)) {
                deletePreview();
                lastStatus = (Status)getStatus();
                setStatus(SetNumber);
            }
        }
        break;

    case SetNumber: {
            bool ok;
            int n = c.toInt(&ok);
            if (ok==true) {
                if (n>0 && n<100) {
                    number = n;
                } else {
                    PF_DIALOGFACTORY->commandMessage(tr("Not a valid number. "
                                                        "Try 1..99"));
                }
            } else {
                PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
            }
            PF_DIALOGFACTORY->requestOptions(this, true, true);
            setStatus(lastStatus);
        }
        break;

    default:
        break;
    }
}



PF_StringList WF_DrawLineParallelThrough::getAvailableCommands() {
    PF_StringList cmd;

    switch (getStatus()) {
    case SetEntity:
        cmd += command("number");
        break;
    default:
        break;
    }

    return cmd;
}



void WF_DrawLineParallelThrough::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawLineParallelThrough::updateToolBar() {
    if (getStatus()==SetPos && !isFinished()) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarLines);
    }
}


// EOF
